package com.example.common.user;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GlobalUser {
    private static final String TAG = "GlobalUser";
    private static volatile GlobalUser globalUser;
    public static String TOKEN = "";//用户token
    public static Integer ID;//用户ID
    public static String AVATAR_URL;//用户头像url
    public static Boolean IS_LOGIN=false;
    public static String USERNAME;//用户名

    //binding同步
    private MutableLiveData<String> bindAvatarUrl=new MutableLiveData<>();
    private MutableLiveData<String> bindUserName=new MutableLiveData<>();
    private MutableLiveData<Boolean> bindIsLogin=new MutableLiveData<>();
    private MutableLiveData<String> bindIntroduction=new MutableLiveData<>();

    //非同步数据
    private Map<Integer,Boolean> likeMap=new HashMap<>();

    private GlobalUser() {
    }

    /**
     * 获取唯一实例
     *
     * @return
     */
    public static GlobalUser getInstance() {
        if (globalUser == null) {
            synchronized (GlobalUser.class) {
                if (globalUser == null) {
                    globalUser = new GlobalUser();
                }
            }
        }
        return globalUser;
    }

    /**
     * 首次调用，设置token字符串
     *
     * @param context
     * @throws FileNotFoundException
     */
    public void setToken(Context context) throws FileNotFoundException {
        File file = new File(context.getFilesDir(), "token");
        //当文件不存在
        if (!file.exists()) {
            try {
                FileOutputStream fos = context.openFileOutput("token", Context.MODE_PRIVATE);
                fos.write(TOKEN.getBytes());
                fos.close();
                Log.i(TAG, "setToken: 新建文件token");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //获取文件中的资源
        FileInputStream fis = context.openFileInput("token");
        InputStreamReader inputStreamReader = new InputStreamReader(fis, StandardCharsets.UTF_8);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                stringBuilder.append(line);
                line = reader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //存入TOKEN中
            TOKEN = stringBuilder.toString();
        }
    }

    /**
     * 刷新token
     * @param context
     */
    public void refreshToken(Context context) {
        File file=new File(context.getFilesDir(),"token");
        if(file.exists()){
            try {
                FileOutputStream fos= context.openFileOutput("token",Context.MODE_PRIVATE);
                fos.write(TOKEN.getBytes());
                fos.close();
                Log.i(TAG, "refreshToken: 更新文件");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            Log.i(TAG, "refreshToken: 文件不存在");
        }
    }

    /**
     * 清空token
     * @param context
     */
    public void deleteToken(Context context){
        TOKEN="";//清空token
        //刷新token
        refreshToken(context);
        //状态清空
        IS_LOGIN=false;
        USERNAME="";
        ID=-1;
        AVATAR_URL="";
        bindAvatarUrl.setValue("com.batbat");
        bindUserName.setValue("");
        bindIsLogin.setValue(false);
        bindIntroduction.setValue("hello batbat!");
    }

    /**
     * 添加收藏
     * @param id 游戏id
     * @param isLike 收藏状态 true为收藏，false为取消收藏
     */
    public void addFavorMap(Integer id,Boolean isLike){
        likeMap.put(id, isLike);
    }

    /**
     * 取消收藏
     * @param id 游戏id
     */
    public void deleteFavorMap(Integer id){
        if(likeMap.get(id)!=null){
            likeMap.remove(id);
        }
    }

    public MutableLiveData<String> getBindAvatarUrl() {
        return bindAvatarUrl;
    }

    public MutableLiveData<String> getBindUserName() {
        return bindUserName;
    }

    public Map<Integer, Boolean> getLikeMap() {
        return likeMap;
    }

    public MutableLiveData<Boolean> getBindIsLogin() {
        return bindIsLogin;
    }

    public MutableLiveData<String> getBindIntroduction() {
        return bindIntroduction;
    }
}
